package com.portal.incurridos.persistence.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.portal.incurridos.persistence.configuration.Label;
import com.portal.incurridos.persistence.dao.LabelDao;
import com.portal.incurridos.persistence.enumeration.StateTransactionEnum;

/**
 * @author Alan Dávila
 * e-mail ustargab@gmail.com
 * date 26/11/2013 23:58:10
 */
@Component("labelDaoImpl")
public class LabelDaoHibernateImpl implements LabelDao {

	private final Logger logger = Logger.getLogger(LabelDaoHibernateImpl.class);  
	/**
	 * Managment bean of dao layer.
	 */
	@Resource(name = "sessionFactory")
    private SessionFactory factoria;

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.LabelDao#get(java.lang.Integer)
	 */
	public final Label get(final Integer label) {
		Session sesion = null;
		Label labelOut = null;
		try {
			sesion = factoria.getCurrentSession();
			labelOut = (Label) sesion.get(Label.class, label);
			sesion.close();
		} catch (Exception e) {
			labelOut = null;
		}
		return labelOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.LabelDao#create(com.portal.incurridos.persistence.configuration.Label)
	 */
	public final Label create(final Label label) {
		Session sesion = null;
		Transaction tx = null;
		Label labelOut = label;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.save(labelOut);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			labelOut = null;
		}
		return labelOut;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.LabelDao#update(com.portal.incurridos.persistence.configuration.Label)
	 */
	public final StateTransactionEnum update(final Label label) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.update(label);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.UPDATED;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.LabelDao#delete(com.portal.incurridos.persistence.configuration.Label)
	 */
	public final StateTransactionEnum delete(final Label label) {
		Session sesion = null;
		Transaction tx = null;
		try {
			sesion = factoria.getCurrentSession();
			tx = sesion.beginTransaction();
			sesion.delete(label);
			tx.commit();
			sesion.close();
		} catch (Exception e) {
			return StateTransactionEnum.ERROR;
		}
		return StateTransactionEnum.DELETED;
	}

	/* (non-Javadoc)
	 * @see com.portal.incurridos.persistence.dao.LabelDao#getAll()
	 */
	@SuppressWarnings("unchecked")
	public final List<Label> getAll() {
		Session sesion = null;
		List<Label> labels = null;
		try {
			sesion = factoria.getCurrentSession();
			labels = sesion.createQuery("from Label").list();
			sesion.close();
		} catch (Exception e) {
			labels = new ArrayList<Label>();
			logger.error("Can't find labels");
		}
		return labels;
	}

}
